//
//  DJDetailBannerViewController.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/18.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "DJDetailBannerViewController.h"
#import "DJDetailViewController.h"
#import "MainViewController.h"
#import "GXNetTool.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
#import "CFMacro.h"
// pop三方
#import "UINavigationController+FDFullscreenPopGesture.h"

#import "ProgramRoot.h"
#import "ProRecord_Dj_Info.h"
#import "ProRecord_Play_Info.h"
#import "ProRecords_Info.h"
#import "ProRecords.h"
#import "ProgramListTableViewCell.h"
#import "Record_Plays.h"
#import "Dj_Info.h"


#define kProgramListTableViewCell @"ProgramListTableViewCell"

@interface DJDetailBannerViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView *avatarView;
@property (nonatomic, strong) UIViewController *vc;
@property (nonatomic, strong) ProgramRoot *proDetailRoot;
@property (weak, nonatomic) IBOutlet UIImageView *backImageView;
@property (weak, nonatomic) IBOutlet UIImageView *frontImageView;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImage;
@property (weak, nonatomic) IBOutlet UILabel *userNick;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UILabel *hzLabel;
@property (weak, nonatomic) IBOutlet UILabel *defaultTitle;
@property (weak, nonatomic) IBOutlet UILabel *defaultName;
@property (weak, nonatomic) IBOutlet UILabel *pageTitler;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *title751;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *title750;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *back751;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *back750;
@property (nonatomic, strong) UILabel *countLabel;

@end

@implementation DJDetailBannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     * tableView
     */
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = [self createHeaderView];
    kTalbleViewRegisterNib(@"ProgramListTableViewCell", kProgramListTableViewCell);
    _userNick.alpha = 0;
    _avatarView.alpha = 0;
    _defaultName.alpha = 0;
    /**
     * 处理数据
     */
    NSString *url = [NSString stringWithFormat:@"http://api.kufm.cn/v3/playback/record_play/%ld/detail_info?page_size=20&page_index=0&flag_version=new", _playKey];
    [GXNetTool GET:url
           andBody:nil
         andHeader:nil
       andResponse:GXJSON
   andSuccessBlock:^(id result) {
       self.proDetailRoot = [ProgramRoot modelWithDictionary:result];
       [self.tableView reloadData];
       [self setValuesForSubViews];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"programTableViewCell 请求数据失败");
    }];
    // 隐藏navigationBar - 三方
    self.fd_prefersNavigationBarHidden = YES;
    [self blurBackImage];
    _avatarView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoDJDetailVC)];
    [_avatarView addGestureRecognizer:tap];
}
- (void)setValuesForSubViews {
    _countLabel.text = [NSString stringWithFormat:@"播放全部(%lu期)", _proDetailRoot.records_info.records.count];
    [_frontImageView sd_setImageWithURL:[NSURL URLWithString:_proDetailRoot.record_play_info.record_play_image_url] placeholderImage:[UIImage imageNamed:@"placeHolderimage"]];
    [_backImageView sd_setImageWithURL:[NSURL URLWithString:_proDetailRoot.record_play_info.record_play_image_url]];
    // 包含作者头像等信息时
    if (_proDetailRoot.record_play_info.record_dj_info.count > 0) {
        _userNick.text = _proDetailRoot.record_play_info.record_dj_info[0].dj_name;
        if ([_proDetailRoot.record_play_info.record_dj_info[0].dj_name isEqualToString:@""]) {
            _userNick.text = _proDetailRoot.record_play_info.record_play_dj;
        }
        [_avatarView sd_setImageWithURL:[NSURL URLWithString:_proDetailRoot.record_play_info.record_dj_info[0].dj_image_url]
                       placeholderImage:[UIImage imageNamed:@"img_dj_head"]];
        _defaultTitle.alpha = 0;
        _userNick.alpha = 1;
        _avatarView.alpha = 1;
    } else {
        _defaultName.alpha = 1;
    }
    _pageTitler.text = _proDetailRoot.record_play_info.record_play_name;
    // 有内容时显示
    if (_proDetailRoot.record_play_info.record_play_decription.length > 0) {
        _descLabel.text = [NSString stringWithFormat:@"简介 :%@", _proDetailRoot.record_play_info.record_play_decription];
    }
    if (_proDetailRoot.record_play_info.play_time_week.count > 0) {
        _detailLabel.text = _proDetailRoot.record_play_info.record_play_modify_at;
    } else {
        _detailLabel.gx_height = 0;
        _title751.priority = 750;
        _title750.priority = 751;
        _containerView.alpha = 0;
        _back750.priority = 751;
        _back751.priority = 750;
    }
    _titleLabel.text = _proDetailRoot.record_play_info.channel_name;
    _hzLabel.text = [NSString stringWithFormat:@"FM%@", _proDetailRoot.record_play_info.channel_hz];
}
- (UIView *)createHeaderView {
    /**
     * 容器view
     */
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [playBtn setImage:[UIImage imageNamed:@"btn_pause_nor"] forState:UIControlStateNormal];
    [playBtn setImage:[UIImage imageNamed:@"btn_pause_hl"] forState:UIControlStateHighlighted];
    [playBtn setFrame:CGRectMake(5, 5, 30, 30)];
    /**
     * 这个label应该写成属性
     */
    self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, SCREEN_WIDTH * 2 / 3.0, 40)];
    _countLabel.font = [UIFont systemFontOfSize:15];
    UILabel *sort = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 50, 10, 50, 20)];
    sort.text = @"排序";
    sort.font = kFont11;
    UIButton *sortBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sortBtn setFrame:CGRectMake(SCREEN_WIDTH - 50 - 20, 10, 20, 20)];
//    sortBtn.backgroundColor = [UIColor redColor];
    UILabel *download = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - sort.gx_width - sortBtn.gx_width * 2 - 20, 10, sort.gx_width, 20)];
    download.text = @"下载";
    download.font = kFont11;
    UIButton *downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [downloadBtn setFrame:CGRectMake(SCREEN_WIDTH - sort.gx_width - sortBtn.gx_width * 2 - 20 * 2, 10, 20, 20)];
//    downloadBtn.backgroundColor = [UIColor redColor];
    [header addSubview:playBtn];
    [header addSubview:_countLabel];
    [header addSubview:downloadBtn];
    [header addSubview:download];
    [header addSubview:sortBtn];
    [header addSubview:sort];
    return header;
}
#pragma mark - tableView 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _proDetailRoot.records_info.records.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_proDetailRoot) {
        ProgramListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kProgramListTableViewCell];
        cell.record = _proDetailRoot.records_info.records[indexPath.row];
        return cell;
    } else {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 49;
}
#pragma mark - 毛玻璃
- (void)blurBackImage {
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.frame = CGRectMake(0, 0, _backImageView.gx_width, 260);
    [_backImageView addSubview:effectview];
}
#pragma mark - 点击头像的触发事件
- (void)gotoDJDetailVC {
    DJDetailViewController *djDetailVC = [[DJDetailViewController alloc] init];
    /**
     * 
     property (nonatomic, strong) NSString *dj_summary;
     @property (nonatomic, strong) NSString *dj_image_url;
     @property (nonatomic, assign) NSInteger dj_id;
     @property (nonatomic, assign) NSInteger dj_listen_num;
     @property (nonatomic, assign) NSInteger dj_popularity;
     @property (nonatomic, assign) NSInteger dj_notice_status;
     @property (nonatomic, strong) NSString *dj_name;
     @property (nonatomic, assign) NSInteger dj_fans;
     @property (nonatomic, assign) NSInteger dj_status;
     */
    Dj_Info *info = [[Dj_Info alloc] init];
    info.dj_id = _proDetailRoot.record_play_info.record_dj_info[0].dj_id;
    info.dj_name = _proDetailRoot.record_play_info.record_dj_info[0].dj_name;
    info.dj_summary = _proDetailRoot.record_play_info.record_play_decription;
    djDetailVC.dj_info = info;
    UINavigationController *navi = (UINavigationController *)self.vc;
    if (navi.viewControllers.count == 4) {
        [navi popViewControllerAnimated:YES];
    } else {
        [navi pushViewController:djDetailVC animated:YES];
    }
}
#pragma mark - 获取控制器
- (UIViewController *)vc {
    id responder = self.nextResponder;
    while (![responder isKindOfClass:[UIViewController class]] && responder != nil) {
        responder = [responder nextResponder];
    }
    return (UIViewController *)responder;
}
- (IBAction)goBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
